
const sql = require('mssql');
const baseFn = require('../../public/base.js');

let nowTimeHalf = baseFn.getNowTimeId().halfDate;
let nowTimeFull = baseFn.getNowTimeId().fullDate;

// 获取供应商信息
const getSupplierInfo = async (ctx) => {
    try {
        const { supplier } = ctx.request.query;
        let selectSql = ` SELECT BM,MC FROM BM_WLDW WHERE DWLX = 1 `;
        if (supplier) {
            selectSql += ` AND (MC LIKE '%${supplier}%' OR JP LIKE '%${supplier}%') `;
        }
        let result = await sql.query(selectSql);
        if (result.rowsAffected[0] > 0) {
            ctx.body = {
                code: 200,
                message: '获取成功',
                result: result.recordset
            }
        } else {
            ctx.body = {
                code: 400,
                message: '获取失败'
            }
        }

    } catch (err) {
        let message = 'originalError' in err ? err.originalError.info.message : '服务器错误'
        ctx.body = {
            code: 500,
            message
        }
    }
}

// 保存供应商获取的号码
const keepGetNumber = async (ctx) => {
    try {
        const { supplierCode } = ctx.request.body;
        let id = await baseFn.createCallNumberId('call_number');
        let code = Number(id.slice(6))
        let keepNumberSql = ` insert into call_number(id,get_number_time,supplier_code,state,code)  SELECT '${id}','${nowTimeFull}',${supplierCode},'get',${code}`;
        let result = await sql.query(keepNumberSql);
        if (result.rowsAffected[0] > 0) {
            ctx.body = {
                code: 200,
                message: '取号成功',
                result: {
                    code
                }
            }
        } else {
            ctx.body = {
                code: 400,
                message: '取号失败'
            }
        }

    } catch (err) {
        let message = 'originalError' in err ? err.originalError.info.message : '服务器错误'
        ctx.body = {
            code: 500,
            message
        }
    } 
}

// 获取当天的取号信息
const getGetNumberInfo = async (ctx) => {
    try {
        const { pageIndex, pageSize, state } = ctx.request.query;

        let selectSql = ` SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY num.id ${state !== 'get' ? 'DESC' : ''}) AS px,num.id,num.get_number_time,num.code,num.supplier_code,dw.MC 
            FROM call_number num left join BM_WLDW dw on num.supplier_code = dw.BM WHERE get_number_time >= '${nowTimeHalf} 00:00:00' `;
        if (state) {
            selectSql += ` AND num.state = '${state}' `;
        }
        selectSql += ` ) t1 WHERE t1.px between ${(pageIndex - 1) * pageSize + 1} and ${pageIndex * pageSize} `;
        let result = await sql.query(selectSql);
        if (result.rowsAffected[0] > 0) {
            ctx.body = {
                code: 200,
                message: '获取数据成功',
                result: result.recordset
            }
        } else {
            ctx.body = {
                code: 400,
                message: '获取数据失败'
            }
        }

    } catch (err) {
        let message = 'originalError' in err ? err.originalError.info.message : '服务器错误'
        ctx.body = {
            code: 500,
            message
        }
    }
}


// 获取正在进行的和即将进行的号
const getNowNextcode = async (ctx) => {
    try {
        let selectSql = ` SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY num.id) AS px,num.id,num.get_number_time,num.code,num.supplier_code,dw.MC 
        FROM call_number num left join BM_WLDW dw on num.supplier_code = dw.BM WHERE state = 'get' AND  get_number_time >= '${nowTimeHalf} 00:00:00') t1 WHERE t1.px between 1 and 2 `;
        let result = await sql.query(selectSql);
        if (result.rowsAffected[0] > 0) {
            ctx.body = {
                code: 200,
                message: '获取数据成功',
                result: result.recordset
            }
        } else {
            ctx.body = {
                code: 400,
                message: '获取数据失败'
            }
        }

    } catch (err) {
        let message = 'originalError' in err ? err.info.message : '服务器错误'
        ctx.body = {
            code: 500,
            message
        }
    }
}



module.exports = {
    'GET /getSupplierInfo': getSupplierInfo,
    'GET /getGetNumberInfo': getGetNumberInfo,
    'GET /getNowNextcode': getNowNextcode,
    'POST /keepGetNumber': keepGetNumber,

}